package test;

import java.io.File;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

public class XMLValidation {
	public static void main(String[] args) throws Exception {
		String xmlFile = "data.xml";
		URL schemaUrl = new URL("http://localhost/xsd/data.xsd");

		// Create schema
		SchemaFactory xsdFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = xsdFactory.newSchema(schemaUrl);

		// validate
		Validator validator = schema.newValidator();
		validator.validate(new StreamSource(new File(xmlFile)));

		/*
		 * Alternative validation with DOMSource DocumentBuilder parser =
		 * DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document
		 * document = parser.parse(new File(xmlFile)); validator.validate(new
		 * DOMSource(document));
		 */
	}
}
